#include "stdafx.h"
#include <cstring>
#include "irender/settings.h"
#include <imglib/io.h>
#include <mswin/file_dir.h>
#include <mswin/seq_name_factory.h>
#include "avi_writer.h"
#include "seq_writer.h"
#include "field_writer.h"

namespace irender {

inline writer_aptr wrap_field_writer(field_render f, writer_aptr wraped)
{
	switch (f) {
	case field_off:
		return wraped;
	case field_upper_first:
		return writer_aptr(new field_writer(wraped, true));
	case field_lower_first:
		return writer_aptr(new field_writer(wraped, false));
	default:
		assert(false);
		return writer_aptr();
	}
}

writer_aptr settings::silent_open_writer(
	size_extent const & dim, fraction frame_per_sec, string name)
{
	string user_format;
	mswin::splitpath(name, 0, 0, 0, &user_format);
	if (user_format.size() > 0)
		user_format = user_format.substr(1);

	if (field != field_off)
		frame_per_sec.den *= 2; //interlacing halves framerate

	bool avi = _stricmp(user_format.c_str(), "avi") == 0;
	if (avi) {
		if (!encoder.is_open()) {
			AfxMessageBox("Please choose AVI coder in Rendering Settings dialog.", 
				MB_ICONEXCLAMATION);
			return writer_aptr();
		}
		return wrap_field_writer(field, 
			writer_aptr(new avi_writer(encoder, name, 
				img::avi::format(dim, frame_per_sec, alpha))));
	}

	if (_stricmp(user_format.c_str(), format.c_str()) != 0) {
		img::ext_vec v;
		img::our_gdiplus_multiwriter().formats(v);
		if (!v.inside(user_format)) {
			AfxMessageBox("Entered format (file extension) is not supported.", 
				MB_ICONEXCLAMATION);
			return writer_aptr();
		}
	}

	return wrap_field_writer(field, 
		writer_aptr(new seq_writer(name, alpha, user_format)));
}

writer_aptr settings::open_writer(
	size_extent const & dim, fraction frame_per_sec,
	string proposed_name, 
	string const & dlg_title, HWND hParent)
{
	string f;
	f = "AVI Video (*.avi)|*.avi|";
	f += "Image Sequence (";
	f += img::our_gdiplus_multiwriter().search_str();
	f += ")|";
	f += img::our_gdiplus_multiwriter().search_str();
	f += "||";

	mswin::change_ext(proposed_name, format);
	bool avi = _stricmp(format.c_str(), "avi") == 0;

	if (!avi && !proposed_name.empty())
		proposed_name = mswin::seq_name_factory::propose_pattern(proposed_name);

	CWnd * pParent = (hParent == 0) ? AfxGetMainWnd() : CWnd::FromHandle(hParent);

	CFileDialog SaveDlg(FALSE, format.c_str(), 
		proposed_name.c_str(), 
		OFN_HIDEREADONLY | OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT | OFN_NOCHANGEDIR | OFN_ENABLESIZING, //| OFN_DONTADDTORECENT (undefined symbol?)
		f.c_str(), pParent);

	if (dlg_title.empty())
		SaveDlg.m_ofn.lpstrTitle = "Render to"; 
	else
		SaveDlg.m_ofn.lpstrTitle = dlg_title.c_str();

	SaveDlg.m_ofn.nFilterIndex = avi ? 1 : 2;

	if (SaveDlg.DoModal() != IDOK) 
		return writer_aptr();

	if (SaveDlg.m_ofn.lpstrFile)
		proposed_name = SaveDlg.m_ofn.lpstrFile;

	return silent_open_writer(dim, frame_per_sec, proposed_name);
}

} //namespace irender
